<template>
  <div class="search">
    <div class="input-content">
      <i class="find"></i>
      <input
        class="input"
        type="text"
        v-model="keyword"
        placeholder="请输入关键词"
      >
    </div>
    <div class="hot-content" v-if="isShow">
      <span
        v-for="(item, index) in hotsList"
        :key="index"
        class="hotword"
        @click="goSearch(item.first)"
      >
        {{item.first}}
      </span>
    </div>
    <div v-else>
      <div>
        <div
          v-for="(item, idx) in searchList"
          :key="'search'+idx"
          class="song-list"
        >
          <span :class="['rank', {'isred': idx<3}]">
            {{Number(idx+1)>9? Number(idx+1) : '0'+Number(idx+1)}}
          </span>
          <p>{{item.name}}</p>
          <span
            v-for="(artist, idx) in item.artists"
            :key="'artist'+idx"
            class="singer"
          >
            {{artist.name}}
            <!-- 歌手是多人时，添加分割线 -->
            <span v-if="item.artists.length>1 && idx!= item.artists.length-1">/</span>
          </span>
        </div>
      </div>
    </div>
  </div>  
</template>

<script>
export default {
  data() {
    return {
      keyword: '',
      hotsList: [],
      // 是否显示热搜词
      isShow: true,
      searchList: []
    }
  },
  created() {
    this.getHot();
  },
  methods: {
    // 获取热搜词
    getHot() {
      this.$http.get('/search/hot')
        .then(res => {
          // console.log(res.data.result.hots);
          this.hotsList = res.data.result.hots;
        })
        .catch(err => {
          console.log(err);
        })
    },
    // 搜索
    goSearch(word) {
      // 填写对应的热搜词
      this.keyword = word;
      // this.$http('search/?keywords='+word)
      this.$http('search', {params: {keywords: word}})  
        .then(res => {
          // 隐藏热搜词，显示搜索结果
          this.isShow = false;
          this.searchList = res.data.result.songs;
        })
        .catch(err => {
          console.log(err);
        })
    }
  },
}
</script>

<style scoped>
  .input-content {
    position: relative;
  }
  .input {
    display: block;
    width: 300px;
    height: 36px;
    margin: 0 auto;
    background-color: #f4f4f4;
    border: none;
    outline: none;
    border-radius: 18px;
    padding-left: 40px;
  }
  .find {
    display: inline-block;
    width: 18px;
    height: 18px;
    background-image: url('../../static/img/search.png');
    background-size: contain;
    position: absolute;
    left: 30px;
    top: 10px;
  }
  .hot-content {
    padding: 0 10px;
    margin-top: 10px;
  }
  .hotword {
    display: inline-block;
    padding: 4px 8px;
    border: 1px solid #888;
    border-radius: 20px;
    margin: 10px;
    font-size: 12px;
  }
  .song-list {
    border-bottom: 1px solid #f4f4f4;
    font-size: 18px;
    padding-left: 40px;
    position: relative;
  }
  .singer {
    color: #666;
    font-size: 14px;
  }
  .rank {
    position: absolute;
    top: 0;
    left: 8px;
    font-size: 18px;
    line-height: 63px;
  }
  .isred {
    color: #f00;
  }
</style>